﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dala.Helpers.DataGenerator
{
    public class DataGenerator
    {
        #region "All Private Properties"

        private const int NUMBER_OF_DATA_TO_ADD = 5000;
        static readonly Random _random = new Random(DateTime.Now.Millisecond);

        #endregion

        #region "All Private Methods"

        public List<Product> GenerateSampleData()
        {
            List<Product> olist = new List<Product>();
            for (int i = 0; i < NUMBER_OF_DATA_TO_ADD; i++)
            {
                var newProduct = new Product()
                {
                    productId = i,
                    productName = RandomString("Product-Name"),
                    productNumber = RandomString("Product-Number"),
                    addDate = RandomDate(),
                    productPrice = RandomDoubleNumber(0.10, 1000.00),
                    Status = RandomBool()
                };
                olist.Add(newProduct);
            }
            return olist;

        }

        public List<Product> GenerateSampleData(int count)
        {
            List<Product> olist = new List<Product>();
            for (int i = 0; i < count; i++)
            {
                var newProduct = new Product()
                {
                    productId = i,
                    productName = RandomString("Product-Name"),
                    productNumber = RandomString("Product-Number"),
                    addDate = RandomDate(),
                    productPrice = RandomDoubleNumber(0.10, 1000.00),
                    Status = RandomBool()
                };
                olist.Add(newProduct);
            }
            return olist;

        }

        #endregion

        #region "All Static Methods"

        static double RandomDoubleNumber(double from, double to)
        {
            return _random.NextDouble();
        }

        static DateTime RandomDate()
        {
            return new DateTime(RandomNumber(1900, 2012), RandomNumber(1, 12), RandomNumber(1, 28));
        }

        static int RandomNumber(int from, int to)
        {
            var value = _random.Next(from, to);
            return value;
        }

        static bool RandomBool()
        {
            return Convert.ToBoolean(RandomNumber(0, 2));
        }

        static string RandomString(string prefix = "", int length = 20)
        {
            StringBuilder builder = new StringBuilder();
            char ch;
            length -= prefix.Length;
            for (int i = 0; i < length; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * _random.NextDouble() + 97)));
                builder.Append(ch);
            }

            return string.Format("{0}{1}", prefix, builder.ToString());
        }

        #endregion

        #region "Select, Get, Count Methods"

        public List<Product> GetProductListPaged(string orderBy = default(string), int startRowIndex = default(int), int maximumRows = default(int))
        {
            if (string.IsNullOrEmpty(orderBy))
                orderBy = "productId desc";

            List<Product> queryResult = null;

            DataGenerator _dataGenerator = new DataGenerator();
            List<Product> olist = _dataGenerator.GenerateSampleData();

            queryResult = (from product in
                               olist
                           select product).Skip(startRowIndex * maximumRows).Take(maximumRows).ToList();

            if (queryResult != null && queryResult.Count > 0)
                return queryResult;
            else
                return null;
        }

        public int GetproductListPagedCount(string orderBy = default(string), int startRowIndex = default(int), int maximumRows = default(int))
        {
            DataGenerator _dataGenerator = new DataGenerator();
            List<Product> olist = _dataGenerator.GenerateSampleData();
            return (from buzz in olist select buzz).ToList().Count;
        }

        #endregion


    }
}
